<template>
  <div class="zhsy">
    <div class="zhsytop">
      <p>
        <span>您好,</span>
        <span>{{ userName }}</span>
        <span><router-link to="/">退出</router-link></span>
      </p>
      <hr />
      <div class="uploadfile">
        <el-upload
          class="avatar-uploader"
          action="/api/upload"
          auto-upload
          :headers="headers"
          :show-file-list="false"
          :name="fileName"
          :on-success="handleAvatarSuccess"
        >
          <img
            v-if="show"
            :src="this.$store.state.user.user_img"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <div class="headPic">
        <img :src="userImg" />
      </div>
      <h3>当前头像</h3>
    </div>
  </div>
</template>

<script>
export default {
  name: "PersonalZhsy",
  data() {
    return {
      userImg: "",
      userName: "",
      show: false,
      fileName: "myHeadImg",
      headers: { Authorization: localStorage.getItem("token") },
      headChange:1
    };
  },
  watch:{
    headChange:function(){
      this.$router.go(0)
    }
  },
  methods: {
    getUserHeadPic() {
      this.$axios
        .get("/api/users/getUserInfo", {
          params: {
            user_id: localStorage.getItem("user_id"),
          },
        })
        .then((res) => {
          this.userImg =
            "http://localhost:3000/images/" + res.data.results[0].user_pic;
        });
    },
    handleAvatarSuccess(res, file) {
      if(res.code == 200){
        alert('修改头像成功')
        this.$router.go(0)
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  created() {
    this.userName = this.$store.state.user.user_name;
    this.getUserHeadPic();
  },
};
</script>

<style>
.uploadfile input[type="file"] {
  margin: 10px;
}
.headPic img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.zhsytop h3 {
  text-align: center;
  padding-right: 40px;
  margin: 10px;
}
.uploadfile {
  margin: 20px auto;
  width: 300px;
}
.headPic {
  width: 200px;
  height: 200px;
  margin-left: 350px;
  border: 1px solid #ccc;
}
.zhsy {
  width: 87%;
  margin-left: 20px;
  background-clip: red;
}
.zhsytop {
  width: 100%;
  height: 400px;
  border: 1px solid #000;
  overflow: hidden;
}
.zhsytop p {
  margin: 10px 20px;
  font-size: 13px;
}
.zhsytop p:nth-child(1) {
  height: 45px;
  line-height: 45px;
  position: relative;
}
.zhsytop > p:nth-child(1) > span:nth-child(1),
.zhsytop > p:nth-child(1) > span:nth-child(2) {
  font-size: 18px;
  font-weight: 600;
}
.zhsytop > p:nth-child(1) > span:nth-child(3) {
  position: absolute;
  right: 6%;
  top: 9%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>